package errorx

const (
	// DefaultCode defaultCode 一般错误
	DefaultCode = 100001
	// 参数错误
	ParamErrCode = 300001
	// 业务处理错误
	BusinessCode = 310000
)

type CodeError struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
}

type CodeErrorResponse struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
}

func NewCodeError(code int, msg string) error {
	return &CodeError{Code: code, Msg: msg}
}

func NewDefaultErrorMsg(msg string) error {
	return NewCodeError(DefaultCode, msg)
}

func NewDefaultError(err error) error {
	return NewCodeError(DefaultCode, err.Error())
}

/**
 * @Description: 参数错误
 * @Author: LiuQHui
 * @Param msg
 * @Return error
 * @Date  2022-10-20 23:15:42
**/
func NewParamErrorMsg(msg string) error {
	return NewCodeError(ParamErrCode, msg)
}

/**
 * @Description: 参数错误
 * @Author: LiuQHui
 * @Param err
 * @Return error
 * @Date  2022-10-20 23:15:48
**/
func NewParamError(err error) error {
	return NewCodeError(ParamErrCode, err.Error())
}

func (e *CodeError) Error() string {
	return e.Msg
}

func (e *CodeError) Data() *CodeErrorResponse {
	return &CodeErrorResponse{
		Code: e.Code,
		Msg:  e.Msg,
	}
}

/**
 * @Description: 业务错误信息
 * @Author: LiuQHui
 * @Param msg
 * @Return error
 * @Date  2022-10-25 12:00:45
**/
func NewBusErrorMsg(msg string) error {
	return NewCodeError(BusinessCode, msg)
}

func NewBusError(err error) error {
	return NewCodeError(BusinessCode, err.Error())
}
